<template>
	<view style="width: 100%;height: 100%;background-color: #f8f8f8;padding: 0 0 24rpx 0;">

		<!-- 搜索部分 -->
		<view class="search_div" style="display: none;">
			<view class="search_border">
				<input class="et_word" maxlength="24" placeholder="请输入关键字" :value="word" @input="onInputWord" />
				<view style="color: #ccc;">|</view>
				<view style="padding: 24rpx 36rpx 24rpx 24rpx;" @click.stop="onSearch">搜索</view>
			</view>
		</view>

		<!-- 占位部分 -->
		<!-- <view style="width: 100rpx;height: 90rpx;"></view> -->

		<view class="tab_div">
			<view class="tab_item" @click.stop="onSelStatus('1')">
				<view :class="curr_tab == '1' ? 'tab_txt_sel' : 'tab_txt'">待接单</view>
			</view>
			<view class="tab_item" @click.stop="onSelStatus('2')">
				<view :class="curr_tab == '2' ? 'tab_txt_sel' : 'tab_txt'">已接单</view>
			</view>
			<view class="tab_item" @click.stop="onSelStatus('3')">
				<view :class="curr_tab == '3' ? 'tab_txt_sel' : 'tab_txt'">已拒绝</view>
			</view>
			<view class="tab_item" @click.stop="onSelStatus('')">
				<view :class="curr_tab == '' ? 'tab_txt_sel' : 'tab_txt'">全部</view>
			</view>
		</view>

		<view style="width: 100%;height: 1rpx;background-color: #eee;"></view>

		<view class="install_list_div">
			<view class="install_item" v-for="(item,index) in data_list" :key="item.id" @click="onItemClick(item)">
				<view style="display: flex;align-items: center;justify-content: space-between;">
					订单编号：{{item.order_sn}}
					<view v-if="item.status == 1" style="color: deepskyblue;font-weight: bold;">待接单</view>
					<view v-if="item.status == 2" style="color: orange;font-weight: bold;">已接单</view>
					<view v-if="item.status == 3" style="color: orangered;font-weight: bold;">拒绝接单</view>
					<view v-if="item.status == 4" style="color: darkgray;font-weight: bold;">绑定TMS</view>
				</view>
				<view v-if="item.factor_name">发货厂家：{{item.factor_name}}</view>
				<view>发货地址：{{item.sender_area}}{{item.sender_address}}</view>
				<view>发货人：{{item.sender_name}} {{item.sender_mobile}}</view>
				<view>收货地址：{{item.receiver_area}}{{item.receiver_address}}</view>
				<view>收货人：{{item.receiver_name}} {{item.receiver_mobile}}</view>
				<!-- <view>货物信息：电器 20 件</view> -->

				<view class="op_btn_div">
					<view v-if="item.status == 2" class="btn1" @click.stop="onBindTms(item)">绑定运单</view>
					<view class="btn2" @click.stop="onConfirm(item)">订单确认</view>
					<view class="btn1" @click.stop="onItemClick(item)">查看详情</view>
				</view>

			</view>

			<view v-if="!data_list || data_list.length <= 0" style="width: 100%;text-align: center;height: 40vh;line-height: 40vh;">
				暂无数据~
			</view>

		</view>

	</view>
</template>

<script>
	import { getUInfo, timestampToYMDHM } from '@/util/util.js'
	export default {
		data() {
			return {
				data_list: [], // 数据列表
				curr_tab: '', // 记录当前选择的tab代表的状态

				word: '', // 搜索关键字

				page: 1,
				page_size: 20,
			}
		},

		mounted() {
			let that = this
			that.reqFirstData()

			// 事件总线:
			uni.$on('receiveOrder', data => {
				console.log('监听事件receiveOrder,参数 data =', data);
				that.reqFirstData()
			})
			// 事件总线:
			uni.$on('postPlatform', data => {
				console.log('监听事件postPlatform,参数 data =', data);
				that.reqFirstData()
			})

		},

		// 下拉刷新
		onPullDownRefresh() {
			this.word = ''
			this.reqFirstData()
		},

		// 到达底部
		onReachBottom() {
			this.page++
			this.getDataList()
		},

		methods: {

			// 点击数据项
			onItemClick(item) {
				uni.navigateTo({ url: `/pages/mine/waybill/waybill_detail?id=${item.id}` })
			},
			// 确认
			onConfirm(item) {
				uni.navigateTo({ url: `/pages/mine/waybill/waybill_confirm?id=${item.id}` })
			},
			// 绑定TMS
			onBindTms(item) {
				uni.navigateTo({ url: `/pages/mine/waybill/waybill_bind_tms?id=${item.id}` })
			},

			// 请求首页数据
			reqFirstData() {
				this.page = 1
				this.data_list = []
				this.getDataList()
			},

			// 点击搜索
			onSearch() {
				this.reqFirstData()
			},

			// -1 全部
			onSelStatus(status) {
				this.curr_tab = status
				this.reqFirstData()
			},

			// 监听:输入关键字
			onInputWord(e) {
				this.word = e.detail.value
			},

			// 拨打电话
			onPhone(item) {
				if (!item || !item.servicephone) {
					uni.showToast({ icon: 'none', title: '号码有误!' })
					return
				}
				uni.makePhoneCall({ phoneNumber: `${item.servicephone}` })
			},

			// 查看详情
			onDetail(item) {
				uni.navigateTo({ url: `/pages/order_detail/order_detail?detail=${JSON.stringify(item)}` })
			},

			// 接口:数据列表
			async getDataList() {
				let that = this
				let token = getUInfo().token
				uni.showLoading({ title: '加载中', mask: true, })
				let res = await this.$getCompanyOrder({ token, page: this.page, page_size: this.page_size, status: this.curr_tab })
				if (res.code == 1) {
					if (!res.data.data || res.data.data.length <= 0) {
						uni.showToast({ icon: 'none', title: '暂无更多数据' })
						return
					}
					this.data_list = this.data_list.concat(res.data.data)
				} else {
					this.$toast(res.msg)
				}
			},

			timestampToDateFormat(ts) {
				return timestampToYMDHM(1000 * ts)
			},

		}
	}
</script>

<style scoped>
	.tab_div {
		display: flex;
		align-items: center;
		justify-content: space-between;
		width: 100%;
		height: 88rpx;
		position: fixed;
		background-color: white;
	}

	.tab_item {
		flex-grow: 1;
		text-align: center;
		height: 100%;
		line-height: 88rpx;
	}

	.tab_txt {
		font-size: 29rpx;
		font-weight: normal;
		color: #000;
	}

	.tab_txt_sel {
		font-size: 29rpx;
		font-weight: bold;
		/* color: #fdd30d; */
		color: red;
	}

	.search_div {
		position: fixed;
		height: 90rpx;
		width: calc(100%);
		background-color: white;
		z-index: 1;
	}

	.search_border {
		display: flex;
		align-items: center;
		width: calc(100% - 48rpx);
		height: calc(100% - 24rpx);
		margin: 12rpx 24rpx 12rpx 24rpx;
		border: solid 1rpx #ccc;
		border-radius: 888rpx;
	}

	.et_word {
		flex-grow: 1;
		padding: 0 0 0 48rpx;
	}

	.install_list_div {
		display: flex;
		flex-direction: column;
		width: 100vw;
		margin: 88rpx 0 0 0;
		/* margin: 0rpx 0 0 0; */
	}

	.install_item {
		display: flex;
		flex-direction: column;
		width: calc(100% - 80rpx);
		margin: 20rpx 20rpx 0 20rpx;
		padding: 20rpx;
		/* background-color: rgba(249, 233, 198, 1); */
		background-color: white;
		overflow: scroll;
		border-radius: 16rpx;
		font-size: 28rpx;
		color: black;
	}

	/* 认证状态 */
	.audit_0 {
		color: orangered;
		font-size: 26rpx;
	}

	.audit_1 {
		color: #fdd30d;
		font-size: 26rpx;
	}

	/* 操作按钮容器 */
	.op_btn_div {
		display: flex;
		justify-content: flex-end;
		align-items: center;
		margin: 20rpx 0 0 0;
		font-size: 26rpx;
	}

	.btn1 {
		width: 130rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		border: solid #000 1px;
		border-radius: 8rpx;
		margin: 0 0 0 20rpx;
	}

	.btn2 {
		width: 130rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		color: red;
		border: solid red 1px;
		border-radius: 8rpx;
		margin: 0 0 0 20rpx;
	}

	.btn3 {
		width: 130rpx;
		height: 50rpx;
		line-height: 50rpx;
		text-align: center;
		border: solid #000 1px;
		border-radius: 8rpx;
		margin: 0 0 0 20rpx;
	}
</style>